/**
 * Author:yuiop
 * Date:2022/10/20 16:31
 */
package com.devops.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.devops.admin.mapper.DOSKubernetesResourceMapper;
import com.devops.admin.mapper.DOSReleaseMapper;
import com.devops.admin.model.DOSKubernetesResource;
import com.devops.admin.model.DOSRelease;
import com.devops.admin.service.DOSKubernetesResourceService;
import com.devops.admin.service.DOSReleaseOutService;
import com.devops.admin.util.Assert;
import com.devops.admin.util.K8sClientUtil;
import com.devops.admin.vo.DOSK8sReleaseVo;
import com.devops.admin.vo.DOSNamespaceChart;
import com.devops.constant.DevOpsConstant;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DOSReleaseOutServiceImpl implements DOSReleaseOutService {
    @Autowired
    DOSReleaseMapper releaseMapper;
    @Autowired
    DOSKubernetesResourceMapper k8sResourceMapper;

    @Override
    public DOSNamespaceChart dealDbRelease(DOSK8sReleaseVo release) {
        Assert.isTrueMvc(release.getId() != null && release.getId() > 0,"releaseId不能为空");
        DOSNamespaceChart namespaceChart;

        if (release.getType().equals(DevOpsConstant.ReleaseType.HELM)) { // helm部署的release
            namespaceChart = getInfoByReleaseId(release.getId());
            namespaceChart.setKind(release.getKind()); //这一步 记不清了 为什么要这么设置 回头补上注释
        } else if (release.getType().equals(DevOpsConstant.ReleaseType.YAML)) { //yaml部署的
            namespaceChart = getInfoByResourceId(release.getId());
        } else {
            return null;
        }


//        if(namespaceChart == null){
//            return null;
//        }
        Assert.isTrueMvc(namespaceChart != null
                && StrUtil.isNotBlank(namespaceChart.getNamespace()),"请检查releaseId或者kubernetesResourceId是否正确");
        namespaceChart.setReleaseType(release.getType());
        KubernetesClient client = K8sClientUtil.getKubernetesClient(namespaceChart.getConfigPath(), namespaceChart.getConfigContent());
        namespaceChart.setClient(client);
        namespaceChart.setReleaseId(release.getId());
        return namespaceChart;
    }
    @Override
    public DOSNamespaceChart getInfoByReleaseId(int releaseId) {

        return releaseMapper.getInfoById(releaseId);
    }

    @Override
    public DOSNamespaceChart getInfoByResourceId(int resourceId) {

        return k8sResourceMapper.getInfoById(resourceId);
    }

    @Override
    public List<DOSRelease> selectListByReleaseName(String releaseName) {
        return releaseMapper.selectList(Wrappers.<DOSRelease>lambdaQuery()
                .eq(DOSRelease::getReleaseName,releaseName));
    }

    @Override
    public List<DOSKubernetesResource> getK8sList(LambdaQueryWrapper<DOSKubernetesResource> kubernetesResourceName) {
        return k8sResourceMapper.selectList(kubernetesResourceName);
    }

    @Override
    public DOSRelease getReleseById(Integer releaseId) {
        return releaseMapper.selectById(releaseId);
    }

    @Override
    public DOSRelease getReleaseByReleaseName(String releaseName) {
        List<DOSRelease> dosReleaseList = releaseMapper.selectList(Wrappers.<DOSRelease>lambdaQuery()
                .eq(DOSRelease::getReleaseName,releaseName));
        if (CollUtil.isEmpty(dosReleaseList)) {
            return null;
        }
        return dosReleaseList.get(0);
    }

    @Override
    public DOSKubernetesResource getKubernetesResourceByResourceName(String resourceName) {
        return k8sResourceMapper.selectOne(Wrappers.<DOSKubernetesResource>lambdaQuery()
                .eq(DOSKubernetesResource::getName,resourceName));
    }

}
